package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Environment;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.TrainingVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadLargeFileListener;
import com.liulishuo.filedownloader.FileDownloader;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/2/28 11:18
 * 修改备注：
 * ================================================
 */
public class TrainingQRCodeActivity extends BaseActivity {

    @Bind(R.id.qr_code_sdv)
    SimpleDraweeView qrCodeSdv;
    TrainingVO trainingVO;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_qr_code);
        setTitle("二维码信息");
        setRightText("保存");
    }

    @Override
    public void initMember() {

        trainingVO = (TrainingVO) getIntent().getSerializableExtra("TrainingVO");
        if (trainingVO == null)
            return;

        FrescoUtil.showQRCode(trainingVO.getTrain_QRcode(), qrCodeSdv);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {

    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

        if (trainingVO == null) {
            showToast("图片路径获取失败!");
            return;
        }

        String url = URL.SERVER + "FileService/showQRCode?moduleName=train&fileName=" + trainingVO.getTrain_QRcode();
        final String resPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Downloads" + File.separator;

        FileDownloader.getImpl().create(url)
                .setPath(resPath + trainingVO.getTrain_QRcode())
                .setListener(new FileDownloadLargeFileListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, long soFarBytes, long totalBytes) {
                        LogUtils.e("pending");
                    }

                    @Override
                    protected void progress(BaseDownloadTask task, long soFarBytes, long totalBytes) {
                        LogUtils.e("progress" + soFarBytes + " : " + totalBytes);
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, long soFarBytes, long totalBytes) {
                        LogUtils.e("paused");
                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        showToast("下载成功,文件路径\n" + resPath);
//                        openFile(OrdersDetailsActivity.this, task.getTargetFilePath());
//                        MIMETypeUtil.openFile(TrainingQRCodeActivity.this, new File(task.getTargetFilePath()));
                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        LogUtils.e("error");
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {
                        LogUtils.e("warn");
                    }
                }).start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
